<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>菜单管理</title>
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
                <button class="layui-btn layui-btn-warm" id="add">添加顶级菜单</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn layui-btn-xs" lay-event="add">增下级</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 定义了员工新增时弹出窗口时的模板 -->
<script type="text/html" id="menuTem">
    <form class="layui-form" action="" lay-filter="menuForm" enctype="multipart/form-data">
        <input type="hidden" id="id" name="id">
        <input type="hidden" id="parentId" name="parentId">

        <div class="layui-form-item">
            <label class="layui-form-label">菜单名称</label>
            <div class="layui-input-inline">
                <label>
                    <input type="text" name="name" placeholder="请输入菜单名称" autocomplete="off" class="layui-input">
                </label>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">菜单地址</label>
            <div class="layui-input-inline">
                <label>
                    <input type="text" name="href" placeholder="请输入菜单地址" autocomplete="off"
                           class="layui-input">
                </label>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">菜单图标</label>
            <div class="layui-input-inline">
                <label>
                    <input type="text" name="icon" placeholder="请输入菜单图标" autocomplete="off" class="layui-input">
                </label>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">Target</label>
            <div class="layui-input-inline">
                <label>
                    <input type="text" name="target" placeholder="请输入Target" autocomplete="off" class="layui-input">
                </label>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">排序号</label>
            <div class="layui-input-inline">
                <label>
                    <input type="text" name="sort" placeholder="请输入排序号" autocomplete="off" class="layui-input">
                </label>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button id="saveBtn" type="button" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交
                </button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>
</script>

<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script th:inline="none">
    layui.use(['table', 'treetable', 'common', 'form'], function () {
        let $ = layui.jquery,
            table = layui.table,
            treetable = layui.treetable,
            form = layui.form;

        let tableRender = function () {
            // 渲染表格
            layer.load(2);
            treetable.render({
                treeColIndex: 1,
                // 一级分类的上级 ID 是几
                treeSpid: 0,
                // 哪个属性是 主键
                treeIdName: 'id',
                // 上级id
                treePidName: 'parentId',
                elem: '#munu-table',
                url: '/adminMenu/list',
                page: false,
                cols: [[
                    {type: 'numbers'},
                    {field: 'name', minWidth: 200, title: '菜单名称'},
                    {field: 'href', title: '菜单url'},
                    {field: 'target', title: 'Target'},
                    {field: 'icon', title: '图标'},
                    {field: 'sort', width: 80, align: 'center', title: '排序号'},
                    {templet: '#auth-state', width: 220, align: 'center', title: '操作'}
                ]],
                done: function () {
                    layer.closeAll('loading');
                }
            });
        };
        tableRender();

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        // 添加或修改数据的保存操作
        $("body").on("click", "#saveBtn", function () {
            let data = form.val("menuForm");
            $.post("/adminMenu/save", data, function () {
                tableRender();
                layer.closeAll();
                if (data.id === null || data.id === '') {
                    layer.msg("添加成功", {
                        icon: 1,
                        time: 1000
                    });
                } else {
                    layer.msg("修改成功", {
                        icon: 1,
                        time: 1000
                    });
                }
            });
        });


        let addMenu = function (id) {
            layer.open({
                type: 1,
                area: ['460px', '450px'],
                title: '添加菜单',
                content: $("#menuTem").html()
            });
            $("#parentId").val(id);
        };

        // 添加顶级菜单
        $("#add").on("click", function () {
            addMenu(0);
        });
        // 监听工具条
        table.on('tool(munu-table)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;

            if (layEvent === 'del') {
                layer.confirm("您确定要删除该菜单吗?", function () {
                    $.post("/adminMenu/del", {id: obj.data.id}, function () {
                        tableRender();
                        layer.closeAll();
                        layer.msg("删除成功！", {
                            icon: 1,
                            time: 1000
                        });
                    });
                });
            }
            else if (layEvent === 'edit') {
                $.get("/adminMenu/" + data.id, function (m) {
                    layer.open({
                        type: 1,
                        area: ['460px', '450px'],
                        title: '编辑菜单',
                        content: $("#menuTem").html()
                    });
                    form.val("menuForm", m);
                });

            }
            else if (layEvent === 'add') {
               addMenu(data.id);
            }
        });
    });
</script>
</body>
</html>